/**   
 * @Title: ShuangseqiuChooseHemaiAct.java 
 * @Package com.esun.rabbit2.viewer.mainact.lotteryhallact.shuangseqiuagp 
 * @Description:(双色球发起合买) 
 * @author: City Jiangsy@500wan.com   
 * @date: 2011-6-29 下午12:25:18  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact.shuangseqiuagp;

import java.text.DecimalFormat;

import com.esun.config.Config;
import com.esun.config.Language;
import com.esun.model.ShuangseqiuInfo;
import com.esun.model.UserInfo;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.communicaters.ShuangseqiuHemaiLannchCommunicater;
import com.esun.rabbit2.viewer.LoginAct;
import com.esun.rabbit2.viewer.mainact.OptionAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.HelpAct;
import com.esun.rabbit2.viewer.mainact.mylotteryact.recharg.RechargeMenuPage;
import com.esun.ui.R;
import com.esun.util.LoginTimeControl;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @ClassName: ShuangseqiuChooseHemaiAct
 * @Description: (双色球发起合买)
 * @author: City Jiangsy@500wan.com
 * @date: 2011-6-29 下午12:25:18
 * 
 */
public class ShuangseqiuChooseHemaiAct extends Activity implements OnClickListener, OnFocusChangeListener, OnCheckedChangeListener {

	private EditText edtHemaiPartCount;
	private EditText edtHemaiTicheng;
	private EditText edtHemaiRengou;
	private EditText edtHemaiBaodi;
	private EditText edtHemaiMultiple;

	private Button btnHemaiSubmit;
	private ImageButton btnHemaiCancel;
	private ImageButton btnHemaiHelp;

	private TextView tvHemaiMoneyAll;
	private TextView tvHemaiMoneyEach;
	private TextView tvHemaiRengou;
	private TextView tvHemaiBaodi;
	private TextView tvHemaiPercentRengou;
	private TextView tvHemaiPercentBaodi;

	private CheckBox cbxHemaiIsBaodi;
	private RadioGroup radioGroupIsOpen;

	private Handler handlerHemai;
	private final static int HEMAI_SUCCEED = 1001; // 标识位,购买成功
	private final static int HEMAI_FAIL = 1002; // 标识位,购买失败

	private int multiple; // 倍数
	private int betCountAll; // 注数
	private int openType; // 公开状态 0完全公开1截止后公开2仅跟单人可看
	private int allNum; // 总份数
	private int buyNum; // 认购份数 ,不低于总份数5%,不高于总份数
	private int baodiNum; // 保底份数,占总份数不低于20%
	private int ticheng; // 0,1,2,3,4,5,6,7,8,9,10 0为缺省值

	private double eachMoney; // 每份金额,保留小数点后两位

	private ProgressDialog progressdialogHemai;

	private boolean isRightAllNum = true;
	private boolean isRightBuyNum = true;
	private boolean isRightBaodiNum = true;

	private Dialog dialogBuySucceed;
	private LoginTimeControl loginTimeControl;

	private ShuangseqiuHemaiLannchCommunicater shuangseqiuHemaiLannchCommunicater;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shuangseqiu_choose_hemai_act);
		initUi();
		edtHemaiMultiple.requestFocus();
		initHandler();
	}

	private void initHandler() {
		handlerHemai = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				progressdialogHemai.dismiss();
				switch (msg.what) {
				case HEMAI_SUCCEED:
					ShuangseqiuInfo.getShuangseqiuInfo().getLotteryList().clear();
					buySucceed();
					break;

				case HEMAI_FAIL:
					if (shuangseqiuHemaiLannchCommunicater.getState() == 0) {
						ViewerUtil.showTipDialog(ShuangseqiuChooseHemaiAct.this, shuangseqiuHemaiLannchCommunicater.getServerMsg() + Config.COMMA + Language.POPLOTTERYTIP_LOOKOVERRECORD, false);
					} else if (shuangseqiuHemaiLannchCommunicater.getState() == 105) {
						popBalanceNotEnoughAlert();
					} else {

						// ViewerUtil.showTipDialog(ShuangseqiuChooseHemaiAct.this, shuangseqiuHemaiLannchCommunicater.getErrorMSG(), false);
						Toast.makeText(ShuangseqiuChooseHemaiAct.this, shuangseqiuHemaiLannchCommunicater.getServerMsg(), Toast.LENGTH_SHORT).show();
					}
					break;

				case Config.CONNECTION_FAIL:
					// ViewerUtil.showTipDialog(ShuangseqiuChooseHemaiAct.this, Language.STR_CONNECTIONFAIL, false);
					Toast.makeText(ShuangseqiuChooseHemaiAct.this, Language.STR_CONNECTIONFAIL, Toast.LENGTH_SHORT).show();
					break;

				default:
					break;
				}
			}
		};

	}

	private Dialog dialogBalanceNotEnough;
	private View.OnClickListener moneyNotEnoughListener;

	private void popBalanceNotEnoughAlert() {
		if (dialogBalanceNotEnough == null) {
			dialogBalanceNotEnough = new Dialog(this, R.style.dialog);
		}

		if (moneyNotEnoughListener == null) {
			moneyNotEnoughListener = new OnClickListener() {

				@Override
				public void onClick(View v) {
					switch (v.getId()) {
					case R.id.shiyixuanwu_alert_money_not_enough_btn_cancel:
						break;

					case R.id.shiyixuanwu_alert_money_not_enough_btn_recharge:
						UserInfo.getUserInfo().setBuyingRecharge(true);

						ShuangseqiuChooseHemaiAct.this.startActivity(new Intent().setClass(ShuangseqiuChooseHemaiAct.this, RechargeMenuPage.class));

						break;
					}

					dialogBalanceNotEnough.dismiss();
				}
			};
		}

		View view = this.getLayoutInflater().inflate(R.layout.shiyixuanwu_alert_money_not_enough, null);

		((Button) view.findViewById(R.id.shiyixuanwu_alert_money_not_enough_btn_cancel)).setOnClickListener(moneyNotEnoughListener);
		((Button) view.findViewById(R.id.shiyixuanwu_alert_money_not_enough_btn_recharge)).setOnClickListener(moneyNotEnoughListener);

		dialogBalanceNotEnough.setContentView(view);
		dialogBalanceNotEnough.show();
	}

	private void initUi() {
		edtHemaiBaodi = (EditText) findViewById(R.id.shuangseqiuchoosehemaiact_edt_baodi);
		edtHemaiBaodi.setOnFocusChangeListener(this);
		edtHemaiPartCount = (EditText) findViewById(R.id.shuangseqiuchoosehemaiact_edt_partCount);
		edtHemaiPartCount.setOnFocusChangeListener(this);
		edtHemaiRengou = (EditText) findViewById(R.id.shuangseqiuchoosehemaiact_edt_rengou);
		edtHemaiRengou.setOnFocusChangeListener(this);
		edtHemaiTicheng = (EditText) findViewById(R.id.shuangseqiuchoosehemaiact_edt_ticheng);
		edtHemaiTicheng.setOnFocusChangeListener(this);
		edtHemaiMultiple = (EditText) findViewById(R.id.shuangseqiuchoosehemaiact_edt_multiple);
		edtHemaiMultiple.setOnFocusChangeListener(this);

		btnHemaiCancel = (ImageButton) findViewById(R.id.shuangseqiuchoosehemaiact_btn_cancel);
		btnHemaiCancel.setOnClickListener(this);
		btnHemaiHelp = (ImageButton) findViewById(R.id.shuangseqiuchoosehemaiact_btn_help);
		btnHemaiHelp.setOnClickListener(this);

		btnHemaiSubmit = (Button) findViewById(R.id.shuangseqiuchoosehemaiact_btn_submit);
		btnHemaiSubmit.setOnClickListener(this);

		tvHemaiBaodi = (TextView) findViewById(R.id.shuangseqiuchoosehemaiact_tv_baodi);
		tvHemaiMoneyAll = (TextView) findViewById(R.id.shuangseqiuchoosehemaiact_tv_moneyAll);
		tvHemaiMoneyEach = (TextView) findViewById(R.id.shuangseqiuchoosehemaiact_tv_moneyEach);
		tvHemaiRengou = (TextView) findViewById(R.id.shuangseqiuchoosehemaiact_tv_rengou);
		tvHemaiPercentRengou = (TextView) findViewById(R.id.shuangseqiuchoosehemaiact_tv_percentRengou);
		tvHemaiPercentBaodi = (TextView) findViewById(R.id.shuangseqiuchoosehemaiact_tv_percentBaodi);

		cbxHemaiIsBaodi = (CheckBox) findViewById(R.id.shuangseqiuchoosehemaiact_cbx_isBaodi);
		cbxHemaiIsBaodi.setOnCheckedChangeListener(this);
		cbxHemaiIsBaodi.setChecked(false);
		edtHemaiBaodi.setEnabled(false);

		loginTimeControl = new LoginTimeControl();

		// 方案公开状态,0完全公开,1截止后公开,2仅跟单人可看
		openType = 0;
		radioGroupIsOpen = (RadioGroup) findViewById(R.id.shuangseqiuchoosehemaiact_radiogroup_isOpen);
		radioGroupIsOpen.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.shuangseqiuchoosehemaiact_radiobutton_open:
					openType = 0;
					break;

				case R.id.shuangseqiuchoosehemaiact_radiobutton_openEnd:
					openType = 1;
					break;

				case R.id.shuangseqiuchoosehemaiact_radiobutton_onlyForGendan:
					openType = 2;
					break;
				default:
					break;
				}
			}
		});

		shuangseqiuHemaiLannchCommunicater = new ShuangseqiuHemaiLannchCommunicater();
		progressdialogHemai = ViewerUtil.showProgressDialog(this, Language.POPLOTTERYTIP_BUYING);

		betCountAll = 0;
		for (int i = 0; i < ShuangseqiuInfo.getShuangseqiuInfo().getLotteryList().size(); i++) {
			betCountAll += (Integer) ShuangseqiuInfo.getShuangseqiuInfo().getLotteryList().get(i).get("bet_count");
		}

		multiple = 1;
		openType = 0;
		allNum = 1;
		buyNum = 1;
		baodiNum = 0;
		ticheng = 0;

		eachMoney = (multiple * betCountAll * 200) / allNum;

		updateTvtInfo();
		updateEdtInfo();

		editTextWatcherAdd();
	}

	/**
	 * @Title: updateTvtInfo
	 * @Description: (更新textview中的内容,加入状态判定)
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void updateTvtInfo() {

		tvHemaiMoneyAll.setText(Language.POPLOTTERYTIP_RMB + Integer.toString(betCountAll * multiple * 2));
		tvHemaiMoneyEach.setText(Language.POPLOTTERYTIP_RMB + getDoubleString(eachMoney));
		if (allNum > multiple * betCountAll * 2) {
			isRightAllNum = false;
			tvHemaiMoneyEach.setText(Language.STR_QINGSHURU + Config.NUM1 + Config.HORIZONTAL_LINE + Integer.toString(multiple * betCountAll * 2) + Language.STR_ZHIJIANDESHUZI);
			tvHemaiMoneyEach.setTextColor(getResources().getColor(R.color.dark_red));
		} else if (allNum < 1) {
			isRightAllNum = false;
			tvHemaiMoneyEach.setText(Language.STR_QINGSHURU + Config.NUM1 + Config.HORIZONTAL_LINE + Integer.toString(multiple * betCountAll * 2) + Language.STR_ZHIJIANDESHUZI);
			tvHemaiMoneyEach.setTextColor(getResources().getColor(R.color.dark_red));
		} else if ((multiple * betCountAll * 200) % allNum == 0 && (multiple * betCountAll * 200) / allNum >= 100) {
			isRightAllNum = true;
			eachMoney = (multiple * betCountAll * 200) / allNum;
			tvHemaiMoneyEach.setText(Language.POPLOTTERYTIP_RMB + getDoubleString(eachMoney));
			tvHemaiMoneyEach.setTextColor(getResources().getColor(R.color.solid_black));
		} else {
			isRightAllNum = false;
			tvHemaiMoneyEach.setText(Language.STR_BUNENGBEIZHENGCHU);
			tvHemaiMoneyEach.setTextColor(getResources().getColor(R.color.dark_red));
		}

		if (buyNum > allNum) {
			isRightBuyNum = false;
			tvHemaiRengou.setText(Language.POPLOTTERYTIP_RMB + getDoubleString(eachMoney * buyNum));
			tvHemaiPercentRengou.setText(Language.STR_QINGSHURU + Integer.toString(getRengouLower(allNum)) + Config.HORIZONTAL_LINE + Integer.toString(allNum) + Language.STR_ZHIJIANDESHUZI);
			tvHemaiPercentRengou.setTextColor(getResources().getColor(R.color.dark_red));
		} else if (buyNum < getRengouLower(allNum)) {
			isRightBuyNum = false;
			tvHemaiRengou.setText(Language.POPLOTTERYTIP_RMB + getDoubleString(eachMoney * buyNum));
			tvHemaiPercentRengou.setText("认购份数不能低于总份数的5%");
			tvHemaiPercentRengou.setTextColor(getResources().getColor(R.color.dark_red));
		} else {
			isRightBuyNum = true;
			tvHemaiRengou.setText(Language.POPLOTTERYTIP_RMB + getDoubleString(eachMoney * buyNum));
			tvHemaiPercentRengou.setText(Config.LEFT_ROUND_BR + getDoubleString((double) (buyNum * 10000 / allNum)) + Config.PERCENT + Config.RIGHT_ROUND_BR);
			tvHemaiPercentRengou.setTextColor(getResources().getColor(R.color.solid_black));
		}
		if (cbxHemaiIsBaodi.isChecked()) {
			if (baodiNum > allNum) {
				isRightBaodiNum = false;
				tvHemaiBaodi.setText(Language.POPLOTTERYTIP_RMB + getDoubleString(eachMoney * baodiNum));
				tvHemaiPercentBaodi.setText(Language.STR_QINGSHURU + Integer.toString(getBaodiLower(allNum)) + Config.HORIZONTAL_LINE + Integer.toString(allNum) + Language.STR_ZHIJIANDESHUZI);
				tvHemaiPercentBaodi.setTextColor(getResources().getColor(R.color.dark_red));
			} else if (baodiNum < getBaodiLower(allNum)) {
				isRightBaodiNum = false;
				tvHemaiBaodi.setText(Language.POPLOTTERYTIP_RMB + getDoubleString(eachMoney * baodiNum));
				tvHemaiPercentBaodi.setText("保底份数不能低于总份数的20%");
				tvHemaiPercentBaodi.setTextColor(getResources().getColor(R.color.dark_red));
			} else {
				isRightBaodiNum = true;
				tvHemaiBaodi.setText(Language.POPLOTTERYTIP_RMB + getDoubleString(eachMoney * baodiNum));
				tvHemaiPercentBaodi.setText(Config.LEFT_ROUND_BR + getDoubleString((double) (baodiNum * 10000 / allNum)) + Config.PERCENT + Config.RIGHT_ROUND_BR);
				tvHemaiPercentBaodi.setTextColor(getResources().getColor(R.color.solid_black));
			}
		} else {
			isRightBaodiNum = true;
			tvHemaiBaodi.setText(Language.POPLOTTERYTIP_RMB + getDoubleString(eachMoney * baodiNum));
			tvHemaiPercentBaodi.setText(Config.LEFT_ROUND_BR + getDoubleString((double) (baodiNum * 10000 / allNum)) + Config.PERCENT + Config.RIGHT_ROUND_BR);
			tvHemaiPercentBaodi.setTextColor(getResources().getColor(R.color.solid_black));
		}

	}

	private void updateEdtInfo() {
		edtHemaiTicheng.setText(Integer.toString(ticheng));
		edtHemaiMultiple.setText(Integer.toString(multiple));
		edtHemaiPartCount.setText(Integer.toString(allNum));
		edtHemaiRengou.setText(Integer.toString(buyNum));
		edtHemaiBaodi.setText(Integer.toString(baodiNum));
	}

	/**
	 * @Title: editTextWatcherAdd
	 * @Description: (加入edittext输入监听)
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void editTextWatcherAdd() {
		edtHemaiMultiple.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// if (edtHemaiMultiple.hasFocus()) {
				EDebug.o(this, "edtHemaiMultiple");
				if (!(String.valueOf(s).equals(Config.NULLMARK)) && s != null) {
					multiple = Integer.parseInt(String.valueOf(s));
					Toast multipleToast = Toast.makeText(ShuangseqiuChooseHemaiAct.this, Language.STR_QINGSHURU + Config.NUM1 + Config.HORIZONTAL_LINE + Integer.toString(99)
							+ Language.STR_ZHIJIANDESHUZI, Toast.LENGTH_SHORT);
					multipleToast.setGravity(Gravity.CENTER, 0, 0);
					if (multiple > 99) {
						multipleToast.show();
						multiple = 99;
						edtHemaiMultiple.setText(Integer.toString(multiple));
					} else if (multiple < 1) {
						multipleToast.show();
						multiple = 1;
						edtHemaiMultiple.setText(Integer.toString(multiple));
					}
				} else {
					multiple = 1;
				}
				updateTvtInfo();
				edtHemaiTicheng.setText(Integer.toString(ticheng));
				// edtHemaiMultiple.setText(Integer.toString(multiple));
				edtHemaiPartCount.setText(Integer.toString(allNum));
				edtHemaiRengou.setText(Integer.toString(buyNum));
				edtHemaiBaodi.setText(Integer.toString(baodiNum));

				// tvHemaiMoneyAll.setText(Integer.toString(betCountAll *
				// multiple * 2));

				// }
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		edtHemaiPartCount.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// if (edtHemaiPartCount.hasFocus()) {
				EDebug.o(this, "edtHemaiPartCount");
				if (edtHemaiPartCount.hasFocus()) {
					if (!(String.valueOf(s).equals(Config.NULLMARK)) && s != null) {
						allNum = Integer.parseInt(String.valueOf(s));
						Toast partCountToast = Toast.makeText(ShuangseqiuChooseHemaiAct.this,
								Language.STR_QINGSHURU + Config.NUM1 + Config.HORIZONTAL_LINE + Integer.toString(multiple * betCountAll * 2) + Language.STR_ZHIJIANDESHUZI, Toast.LENGTH_SHORT);
						partCountToast.setGravity(Gravity.CENTER, 0, 0);
						if (allNum > multiple * betCountAll * 2) {
							allNum = multiple * betCountAll * 2;
							partCountToast.show();
							edtHemaiPartCount.setText(Integer.toString(allNum));
						} else if (allNum < 1) {
							allNum = 1;
							edtHemaiPartCount.setText(Integer.toString(allNum));
							partCountToast.show();
						} else if ((multiple * betCountAll * 200) % allNum == 0 && (multiple * betCountAll * 200) / allNum >= 100) {
							eachMoney = (multiple * betCountAll * 200) / allNum;
						} else {
							EDebug.o(ShuangseqiuChooseHemaiAct.this, "noDivisible");
						}
					} else {
						allNum = 1;
					}
					updateTvtInfo();

				}

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		edtHemaiTicheng.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (edtHemaiTicheng.hasFocus()) {
					if (!(String.valueOf(s).equals(Config.NULLMARK)) && s != null) {
						ticheng = Integer.parseInt(String.valueOf(s));
						if (ticheng > 8) {
							Toast tichengToast = Toast.makeText(ShuangseqiuChooseHemaiAct.this, Language.STR_QINGSHURU + Config.NUM0 + Config.HORIZONTAL_LINE + Integer.toString(8)
									+ Language.STR_ZHIJIANDESHUZI, Toast.LENGTH_SHORT);
							tichengToast.setGravity(Gravity.CENTER, 0, 0);
							tichengToast.show();
							ticheng = 8;
							edtHemaiTicheng.setText(Integer.toString(ticheng));
						}
					} else {
						ticheng = 0;
					}
					updateTvtInfo();
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		edtHemaiRengou.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (edtHemaiRengou.hasFocus()) {
					if (!(String.valueOf(s).equals(Config.NULLMARK)) && s != null) {
						buyNum = Integer.parseInt(String.valueOf(s));
						Toast rengouToast = Toast.makeText(ShuangseqiuChooseHemaiAct.this,
								Language.STR_QINGSHURU + Integer.toString(getRengouLower(allNum)) + Config.HORIZONTAL_LINE + Integer.toString(allNum) + Language.STR_ZHIJIANDESHUZI, Toast.LENGTH_SHORT);
						rengouToast.setGravity(Gravity.CENTER, 0, 0);
						if (buyNum > allNum) {
							rengouToast.show();
							buyNum = allNum;
							edtHemaiRengou.setText(Integer.toString(buyNum));
						} else if (buyNum < getRengouLower(allNum)) {
							rengouToast.show();
						}
					} else {
						buyNum = getRengouLower(allNum);
					}
					updateTvtInfo();
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		edtHemaiBaodi.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (edtHemaiBaodi.hasFocus() && cbxHemaiIsBaodi.isChecked()) {
					if (!(String.valueOf(s).equals(Config.NULLMARK)) && s != null) {
						Toast baodiToast = Toast.makeText(ShuangseqiuChooseHemaiAct.this,
								Language.STR_QINGSHURU + Integer.toString(getBaodiLower(allNum)) + Config.HORIZONTAL_LINE + Integer.toString(allNum) + Language.STR_ZHIJIANDESHUZI, Toast.LENGTH_SHORT);
						baodiToast.setGravity(Gravity.CENTER, 0, 0);

						baodiNum = Integer.parseInt(String.valueOf(s));
						if (baodiNum < getBaodiLower(allNum)) {
							baodiToast.show();

						} else if (baodiNum > allNum) {
							baodiToast.show();
							baodiNum = allNum;
							edtHemaiBaodi.setText(Integer.toString(baodiNum));
						} else {

						}
					} else {
						baodiNum = getBaodiLower(allNum);
					}
					updateTvtInfo();
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

	}

	private void threadHemai() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					boolean isBuySucceed = shuangseqiuHemaiLannchCommunicater.isBuySucceed(betCountAll, multiple, betCountAll * multiple * 2, allNum, buyNum, baodiNum, ticheng, openType);
					if (isBuySucceed) {
						Message msg = handlerHemai.obtainMessage(HEMAI_SUCCEED, null);
						handlerHemai.sendMessage(msg);
					} else {
						Message msg = handlerHemai.obtainMessage(HEMAI_FAIL, null);
						handlerHemai.sendMessage(msg);
					}
				} catch (Exception e) {
					Message msg = handlerHemai.obtainMessage(Config.CONNECTION_FAIL, null);
					handlerHemai.sendMessage(msg);
				}
			}
		}.start();
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		switch (v.getId()) {
		case R.id.shuangseqiuchoosehemaiact_edt_partCount:
			if (!hasFocus) {
				edtHemaiPartCount.setText(Integer.toString(allNum));
				updateTvtInfo();
			}
			break;

		case R.id.shuangseqiuchoosehemaiact_edt_multiple:
			if (!hasFocus) {
				edtHemaiMultiple.setText(Integer.toString(multiple));
				updateTvtInfo();
			}
			break;

		case R.id.shuangseqiuchoosehemaiact_edt_ticheng:
			if (!hasFocus) {
				edtHemaiTicheng.setText(Integer.toString(ticheng));
				updateTvtInfo();
			}
			break;

		case R.id.shuangseqiuchoosehemaiact_edt_rengou:
			if (!hasFocus) {
				edtHemaiRengou.setText(Integer.toString(buyNum));
				updateTvtInfo();
			}
			break;

		case R.id.shuangseqiuchoosehemaiact_edt_baodi:
			if (!hasFocus) {
				edtHemaiBaodi.setText(Integer.toString(baodiNum));
				updateTvtInfo();
			}
			break;

		default:
			break;
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.shuangseqiuchoosehemaiact_btn_cancel:
			ShuangseqiuChooseHemaiAct.this.finish();
			break;

		case R.id.shuangseqiuchoosehemaiact_btn_help:
			Intent intentToHelp = new Intent();
			intentToHelp.setClass(ShuangseqiuChooseHemaiAct.this, HelpAct.class);
			intentToHelp.putExtra("url", Config.WHATIS_HEMAI_WEB_ADD).putExtra("title", "合买介绍").putExtra("havetitle", Config.NUM1);
			startActivity(intentToHelp);

			break;

		case R.id.shuangseqiuchoosehemaiact_btn_submit:
			if (UserInfo.getUserInfo().getUserSession().length() < 3) {
				Intent intent = new Intent();
				intent.setClass(ShuangseqiuChooseHemaiAct.this, LoginAct.class);
				startActivity(intent);
			}
			// else if
			// (Double.parseDouble(UserInfo.getUserInfo().getUserBalance()) ==
			// 0) {
			// ViewerUtil.showZeroMoneyDialog(ShuangseqiuChooseHemaiAct.this);
			// }
			else {
				hemaiSubmit();
			}
			break;

		default:
			break;
		}

	}

	private void hemaiSubmit() {
		if (edtHemaiMultiple.getText().equals(null) || edtHemaiMultiple.getText().toString().equals(Config.NULLMARK)
				|| edtHemaiPartCount.getText().equals(null)
				|| edtHemaiPartCount.getText().toString().equals(Config.NULLMARK)

				// edtHemaiTicheng为空时提成默认为0
				// ||edtHemaiTicheng.getText().equals(null)||edtHemaiTicheng.getText().toString().equals(Config.NULLMARK)
				|| edtHemaiRengou.getText().equals(null) || edtHemaiRengou.getText().toString().equals(Config.NULLMARK) || edtHemaiBaodi.getText().equals(null)
				|| edtHemaiBaodi.getText().toString().equals(Config.NULLMARK)) {
			ViewerUtil.showTipDialog(ShuangseqiuChooseHemaiAct.this, Language.STR_QINGWANZHENGTIANXIE, false);
		} else {
			if (!isRightAllNum || !isRightBaodiNum || !isRightBuyNum) {
				EDebug.o(ShuangseqiuChooseHemaiAct.this, "isRightAllNum= " + isRightAllNum + " isRightBaodiNum= " + isRightBaodiNum + " isRightBuyNum= " + isRightBuyNum);
				ViewerUtil.showTipDialog(ShuangseqiuChooseHemaiAct.this, Language.STR_QINGYUEDUTISHI, false);
			} else if (loginTimeControl.isNeedInputPwd()) {
				loginTimeControl.showInputPasswordDialog(this);
			} else {
				progressdialogHemai.show();
				threadHemai();
			}
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.shuangseqiuchoosehemaiact_cbx_isBaodi:
			if (isChecked) {
				edtHemaiBaodi.setEnabled(true);
				baodiNum = getBaodiLower(allNum);
				edtHemaiBaodi.setText(Integer.toString(baodiNum));
				updateTvtInfo();
			} else {
				edtHemaiBaodi.setEnabled(false);
				edtHemaiBaodi.setText(Config.NUM0);
				baodiNum = 0;
				updateTvtInfo();
			}
			break;

		default:
			break;
		}

	}

	/** 工具类,待合并 */
	private String getDoubleString(double _eachMoney) {
		_eachMoney = _eachMoney / 100;
		DecimalFormat format = new DecimalFormat("0.00");
		format.setMaximumFractionDigits(3);
		String result = format.format(_eachMoney);
		return result;
	}

	private int getBaodiLower(int betCount) {
		int baodiLower = 0;
		if (betCount % 5 == 0) {
			baodiLower = betCount / 5;
		} else {
			baodiLower = (betCount / 5) + 1;
		}
		return baodiLower;
	}

	private int getRengouLower(int betCount) {
		int regouLower = 0;
		if (betCount % 20 == 0) {
			regouLower = betCount / 20;
		} else {
			regouLower = (betCount / 20) + 1;
		}
		return regouLower;
	}

	private void buySucceed() {
		dialogBuySucceed = new Dialog(this, R.style.dialog);
		dialogBuySucceed.setContentView(R.layout.viewerutil_dialog_showbuysucceed);
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_msg)).setText("发起合买成功");
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_term)).setText("发起合买双色球" + ShuangseqiuInfo.getShuangseqiuInfo().getTermNum() + "期");
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_count)).setText("认购:" + buyNum + "份");
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_cost)).setText("消费:" + Language.POPLOTTERYTIP_RMB + getDoubleString(eachMoney * buyNum));
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_balance)).setText("剩余金额:" + Language.POPLOTTERYTIP_RMB + UserInfo.getUserInfo().getUserBalance());
		dialogBuySucceed.show();
		OnClickListener dialogSucceedListener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.viewerutildialogshowbuysucceed_btn_back:
					dialogBuySucceed.dismiss();
					ShuangseqiuChooseHemaiAct.this.finish();
					if (UserInfo.getUserInfo().isRebuy()) {
						UserInfo.getUserInfo().setRebuy(false);
						ShuangseqiuChooseAct.showType = 1;
					} else {
						ShuangseqiuChooseAct.showType = 2;
					}
					break;

				case R.id.viewerutildialogshowbuysucceed_btn_toHall:
					dialogBuySucceed.dismiss();
					ShuangseqiuChooseHemaiAct.this.finish();
					ShuangseqiuChooseAct.showType = 1;
					break;

				default:
					break;
				}

			}
		};
		Button btnBc = (Button) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_btn_back);
		btnBc.setOnClickListener(dialogSucceedListener);
		if (UserInfo.getUserInfo().isRebuy()) {
			btnBc.setText("返回详情");
		}
		((Button) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_btn_toHall)).setOnClickListener(dialogSucceedListener);
		dialogBuySucceed.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				ShuangseqiuChooseHemaiAct.this.finish();
			}
		});

	}

	@Override
	protected void onPause() {
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		UserInfo.getUserInfo().setRechargeSucceed(false);
		super.onResume();
		OptionAct.isLoginSucceed = false;
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
		if (UserInfo.getUserInfo().isBuyingRecharge()) {
			ShuangseqiuChooseHemaiAct.this.finish();
			UserInfo.getUserInfo().setBuyingRecharge(false);
			ShuangseqiuChooseAct.showType = 1;
		}
	}
}
